<?php

/**
 * Class WpMedia
 */
class WpMedia extends CWidget
{
	private static $_assets;

	public $uploader = true;

	public $imageEdit = true;

	public function init()
	{
		parent::init();
		if (!isset(self::$_assets)) {
			self::$_assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets');
		}
	}

	public function run()
	{
		$this->registerScript();
		$this->render('templates');
	}

	protected function registerScript()
	{
		/** @var CClientScript $cs */
		$cs = Yii::app()->getClientScript();
		$cs->registerScript('userSettings', 'var userSettings = ' . CJavaScript::encode($this->userSettings()), CClientScript::POS_HEAD)
			->registerScript('wpUtilSettings', 'var _wpUtilSettings = ' . CJavaScript::encode($this->wpUtilSettings()), CClientScript::POS_HEAD)
			->registerScript('wpMediaModelsL10n', 'var _wpMediaModelsL10n = ' . CJavaScript::encode($this->wpMediaModelsL10n()), CClientScript::POS_HEAD)
			->registerScript('wpMediaViewsL10n', 'var _wpMediaViewsL10n = ' . CJavaScript::encode($this->wpMediaViewsL10n()), CClientScript::POS_HEAD)
			->registerCssFile(self::$_assets . '/css/media-views.css')
			->registerCoreScript('jquery')
			->registerCoreScript('jquery.ui')
			->registerScriptFile(self::$_assets . '/js/underscore.min.js')
			->registerScriptFile(self::$_assets . '/js/backbone.min.js')
			->registerScriptFile(self::$_assets . '/js/utils.js')
			->registerScriptFile(self::$_assets . '/js/wp-util.js', CClientScript::POS_END)
			->registerScriptFile(self::$_assets . '/js/wp-backbone.js', CClientScript::POS_END)
			->registerScriptFile(self::$_assets . '/js/media-models.js', CClientScript::POS_END)
			->registerScriptFile(self::$_assets . '/js/media-views.js', CClientScript::POS_END)
			->registerScriptFile(self::$_assets . '/js/media-editor.js', CClientScript::POS_END);

		if ($this->uploader) {
			$cs->registerScript('pluploadL10n', 'var pluploadL10n = ' . CJavaScript::encode($this->pluploadL10n()), CClientScript::POS_HEAD)
				->registerScript('wpPluploadSettings', 'var _wpPluploadSettings = ' . CJavaScript::encode($this->wpPluploadSettings()), CClientScript::POS_HEAD)
				->registerScriptFile(self::$_assets . '/js/plupload/plupload.full.min.js', CClientScript::POS_END)
				->registerScriptFile(self::$_assets . '/js/plupload/wp-plupload.js', CClientScript::POS_END);
		}

		if ($this->imageEdit) {
			$cs->registerScript('imgEditL10n', 'var imageEditL10n= {error: "无法加载预览图片。请尝试刷新页面。"} ', CClientScript::POS_HEAD)
				->registerCssFile(self::$_assets . '/css/media.css')
				->registerCssFile(self::$_assets . '/js/imgareaselect/imgareaselect.css')
				->registerScriptFile(self::$_assets . '/js/imgareaselect/jquery.imgareaselect.js')
				->registerScriptFile(self::$_assets . '/js/image-edit.js');
		}
	}

	public function userSettings()
	{
		return array(
			'uid' => Yii::app()->getUser()->getId(),
			'time' => time()
		);
	}

	public function pluploadL10n()
	{
		return array(
			'big_upload_failed' => '请尝试使用%1$s标准的浏览器上传工具%2$s来上传这个文件。',
			'big_upload_queued' => '%s超出了您浏览器对高级多文件上传工具所做的大小限制。',
			'crunching' => '处理中…',
			'default_error' => '上传时发生了错误。请稍后再试。',
			'deleted' => '移动到回收站。',
			'dismiss' => '不再显示',
			'error_uploading' => '“%s”上传失败。',
			'file_cancelled' => '文件已取消。',
			'file_exceeds_size_limit' => '%s超过了站点的最大上传限制。',
			'http_error' => 'HTTP错误。',
			'image_dimensions_exceeded' => '该文件超过了最大大小，请使用其它文件。',
			'image_memory_exceeded' => '达到内存限制，请使用小一些的文件。',
			'invalid_filetype' => '不允许上传该类型的文件，请选择其它文件。',
			'io_error' =>  'IO错误。',
			'missing_upload_url' => '配置有误。请联系您的服务器管理员。',
			'not_an_image' => '该文件不是图像，请使用其它文件。',
			'queue_limit_exceeded' => '您向队列中添加的文件过多。',
			'security_error' => '安全错误。',
			'upload_failed' => '上传失败。',
			'upload_limit_exceeded' => '您只能上传一个文件。',
			'upload_stopped' => '上传停止。',
			'zero_byte_file' => '文件为空，请选择其它文件。',
		);
	}

	public function wpPluploadSettings()
	{
		$params = array(
			'wpMedia' => true,
			'File[file]' => true,
			'ajax' => 'file-upload',
			'plupload' => true,
		);

		$request = Yii::app()->getRequest();
		if ($request->enableCsrfValidation) {
			$params[$request->csrfTokenName] = $request->getCsrfToken();
		}

		return array(
			'defaults' => array (
				'runtimes' => "html5,flash,silverlight,html4",
				"file_data_name" => 'File[file]',
				'url' => Yii::app()->createAbsoluteUrl('/file/upload', array('model' => 'File', 'field' => 'file')),
				'flash_swf_url' => self::$_assets . '/js/plupload/plupload.flash.swf',
				'silverlight_xap_url' => self::$_assets . '/js/plupload/plupload.silverlight.xap',
				'filters' => array(
					'max_file_size' => '67108864b',
				),
				'multipart_params' => $params,
				'headers' => array(
					'X_REQUESTED_WITH' => 'XMLHttpRequest',
				)
			),
			'browser' => array (
				'mobile' => false,
				'supported' => true
			),
			'limitExceeded' => false,
		);
	}

	public function wpMediaModelsL10n()
	{
		return array(
			'settings' => array(

			)
		);
	}

	public function wpUtilSettings()
	{
		return array(
			'ajax' => array(
				'url' => Yii::app()->createAbsoluteUrl('ajax'),
			),
		);
	}

	public function wpMediaViewsL10n()
	{
		return array(
			'addMedia' => '添加媒体',
			'addToGallery' => '添加至相册',
			'addToGalleryTitle' => '添加至相册',
			'addToPlaylist' => '添加到音频播放列表',
			'addToPlaylistTitle' => '添加到音频播放列表',
			'addToVideoPlaylist' => '添加到视频播放列表',
			'addToVideoPlaylistTitle' => '添加到视频播放列表',
			'allMediaItems' => '所有多媒体项目',
			'audioAddSourceTitle' =>  '添加音频源',
			'audioDetailsCancel' => '取消编辑',
			'audioDetailsTitle' => '音频详情',
			'audioReplaceTitle' => '替换音频',
			'back' => '返回',
			'cancel' => '取消',
			'cancelGalleryTitle' => '← 取消相册',
			'cancelPlaylistTitle' => '← 取消音频播放列表',
			'cancelVideoPlaylistTitle' => '← 取消视频播放列表',
			'chooseImage' => '选择图像',
			'createGalleryTitle' => '创建相册',
			'createNewGallery' => '创建新相册',
			'createNewPlaylist' => '创建新的播放列表',
			'createNewVideoPlaylist' => '创建新的视频播放列表',
			'createPlaylistTitle' => '创建音频播放列表',
			'createVideoPlaylistTitle' => '创建视频播放列表',
			'cropError' => '在裁剪您的图像时发生了错误。',
			'cropImage' => '裁剪图像',
			'cropYourImage' => '裁剪您的图片',
			'cropping' => '裁剪中…',
			'dragInfo' => '拖放来排序图像。',
			'editGalleryTitle' => '编辑相册',
			'editImage' => '编辑图像',
			'editPlaylistTitle' => '编辑音频播放列表',
			'editVideoPlaylistTitle' => '编辑视频播放列表',
			'imageDetailsCancel' => '取消编辑',
			'imageDetailsTitle' => '图像详情',
			'imageReplaceTitle' => '替换图像',
			'insertFromUrlTitle' => '从URL插入',
			'insertGallery' => '插入相册',
			'insertIntoPost' => '插入至文章',
			'insertMediaTitle' => '插入多媒体',
			'insertPlaylist' => '插入音频播放列表',
			'insertVideoPlaylist' => '插入视频播放列表',
			'mediaLibraryTitle' => '媒体库',
			'noItemsFound' => '找不到条目。',
			'playlistDragInfo' => '拖放来排序曲目。',
			'remove' => '移除',
			'replace' => '替换',
			'returnToLibrary' => '← 回到媒体库',
			'reverseOrder' => '反转顺序',
			'search' => '搜索',
			'select' => '选择',
			'selectAndCrop' => '选择并裁剪',
			'selected' => '已选%d个',
			'setFeaturedImage' => '设为特色图像',
			'setFeaturedImageTitle' => '设为特色图像',
			'skipCropping' => '跳过裁剪',
			'suggestedDimensions' => '推荐的图像尺寸：',
			'update' => '更新',
			'updateGallery' => '更新相册',
			'updatePlaylist' => '更新音频播放列表',
			'updateVideoPlaylist' => '更新视频播放列表',
			'uploadFilesTitle' => '上传文件',
			'uploadImagesTitle' => '上传文件',
			'uploadedToThisPost' => '上传到本文章的',
			'url' => 'URL',
			'videoAddSourceTitle' => '添加视频源',
			'videoAddTrackTitle' => '添加字幕',
			'videoDetailsCancel' => '取消编辑',
			'videoDetailsTitle' => '视频详情',
			'videoPlaylistDragInfo' => '拖放来排序视频。',
			'videoReplaceTitle' => '替换视频',
			'videoSelectPosterImageTitle' => '选择海报图像',
			'warnDelete' => "您将永久删除所选条目。\n点击“取消”停止，点击“确定”删除。",
			'settings' => array(
				'tabs' => array(),
				'tabUrl' => '',
				'mimeTypes' => array(
					'image' => '图片',
					'audio' => "音频",
					'video' => "视频",
				),
				'captions' => true,
				"nonce" => array(
					'sendToEditor' => '940b2920aa',
				),
				"post" => array(
					"nonce" => "20b3469fed",
					"featuredImageId" => -1
				),
				"defaultProps" => array(
					"link" => "file",
					"align" => "",
					"size" =>"",
				),
				"attachmentCounts" => array(
					'audio' => 0,
					'video' => 0,
				),
				'embedExts' => array('mp3','ogg','wma','m4a','wav','mp4','m4v','webm','ogv','wmv','flv'),
				'embedMimes' => array(
					'mp3' => 'audio/mpeg',
					'ogg' => 'audio/ogg',
					'wma' => 'audio/x-ms-wma',
					'm4a' => 'audio/mpeg',
					'wav' => 'audio/wav',
					'mp4' => 'video/mp4',
					'm4v' => 'video/mp4',
					'webm' => 'video/webm',
					'ogv' => 'video/ogg',
					'wmv' => 'video/x-ms-wmv',
					'flv' => 'video/x-flv'
				),
				'contentWidth' => 474
			)
		);
	}
}